/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPart;
import org.eclipse.uml2.uml.ActivityEdge;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.LinkNameDisplayEditPart;
import com.cea.papyrus.core.editpart.connection.NamedGraphEdgeEditPart;
import com.cea.papyrus.core.figures.MiddleEndpointLocator;
import com.cea.papyrus.core.figures.connection.SolidLineFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Manages ActivityEdges
 *
 * @author Gilles Cannenterre
 * @since 4 avr. 07
 */
public class ActivityEdgeEditPart extends NamedGraphEdgeEditPart {

	/** The guard. */
	protected GuardStringDisplayEditPart theGuard;
	
	/** The weight. */
	protected WeightStringDisplayEditPart theWeight;
	
	public ActivityEdgeEditPart(GraphEdge graphEdge, ModelManager modelManager) {
		super(graphEdge, modelManager);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		return new SolidLineFigure();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createChild(java.lang.Object)
	 */
	@Override
	protected EditPart createChild(Object model) {
		if (model instanceof GraphNode) {
			if (((GraphNode)model) == (GraphNode)getModelChildren().get(0)) {
				theNameLabel = new LinkNameDisplayEditPart((GraphNode)getModelChildren().get(0),getUmlElement(),modelManager);
				return theNameLabel;
			}
			if (((GraphNode)model) == (GraphNode)getModelChildren().get(2)) {
				theGuard = new GuardStringDisplayEditPart((GraphNode)getModelChildren().get(2),((ActivityEdge)getUmlElement()).getGuard(),modelManager);
				return theGuard;
			}
			if (((GraphNode)model) == (GraphNode)getModelChildren().get(3)) {
				theWeight = new WeightStringDisplayEditPart((GraphNode)getModelChildren().get(3),((ActivityEdge)getUmlElement()).getWeight(),modelManager);
				return theWeight;
			}
		}
		return super.createChild(model);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphEdgeEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		
		Point middleLabel = null;
		MiddleEndpointLocator lo = null;
		IFigure f = null;
		
		if (theNameLabel != null) {
			theNameLabel.refresh();
			//calculate middle point of the label
			middleLabel =new Point(
					theNameLabel.getGraphNode().getPosition().x-((Label) theNameLabel.getFigure()).getPreferredSize().width/2,
					theNameLabel.getGraphNode().getPosition().y-((Label) theNameLabel.getFigure()).getPreferredSize().height/2
			);
			//caluclate middle point of the link
			lo = new MiddleEndpointLocator ((PolylineConnection)this.getFigure(), middleLabel);
	
			f=theNameLabel.getFigure();
			f.setLocation(theNameLabel.getGraphNode().getPosition());//set label a the good position
			if (theNameLabel.isVisible()){
				this.getContentPane().getLayoutManager().setConstraint(theNameLabel.getFigure(),lo);
			}
		}
		
		if (theGuard != null) {
			theGuard.refresh();
			//calculate middle point of the label
			middleLabel = new Point(
					theGuard.getGraphNode().getPosition().x-((Label) theGuard.getFigure()).getPreferredSize().width/2,
					theGuard.getGraphNode().getPosition().y-((Label) theGuard.getFigure()).getPreferredSize().height/2
			);
			// caluclate middle point of the link
			lo = new MiddleEndpointLocator ((PolylineConnection)this.getFigure(), middleLabel);
			
			f=theGuard.getFigure();
			f.setLocation(theGuard.getGraphNode().getPosition());//set label a the good position
			if (theGuard.isVisible()){
				this.getContentPane().getLayoutManager().setConstraint(theGuard.getFigure(),lo);
			}
		}
		
		if (theWeight != null) {
			theWeight.refresh();
			//calculate middle point of the label
			middleLabel = new Point(
					theWeight.getGraphNode().getPosition().x-((Label) theWeight.getFigure()).getPreferredSize().width/2,
					theWeight.getGraphNode().getPosition().y-((Label) theWeight.getFigure()).getPreferredSize().height/2
			);
			// caluclate middle point of the link
			lo = new MiddleEndpointLocator ((PolylineConnection)this.getFigure(), middleLabel);
				
			f=theWeight.getFigure();
			f.setLocation(theWeight.getGraphNode().getPosition());//set label a the good position
			if (theWeight.isVisible()){
				this.getContentPane().getLayoutManager().setConstraint(theWeight.getFigure(),lo);
			}
		}
	}

}
